package com.qumitech.visualnovel.controller.rest;

import java.util.*;
import com.qumitech.visualnovel.component.util.CommonUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.qumitech.visualnovel.service.gamerecord.GameRecordService;
import com.qumitech.visualnovel.model.dao.console.GameRecordDO;
import com.qumitech.visualnovel.model.dao.app.UserGamingWishBottleStatisticsDO;
import com.qumitech.visualnovel.pubdef.dto.game.GameDailyDataDTO;
import com.qumitech.visualnovel.model.dao.console.MuchangwuyuDO;
import com.qumitech.visualnovel.model.dao.app.UserGamingMuchangwuyuStatisticsDO;
import com.qumitech.visualnovel.model.dao.console.MuchangwuyuTermDO;
import com.qumitech.visualnovel.model.dao.console.UserPropExchangeDO;

@RestController
@RequestMapping("/api/gamerecord")
public class GameRecordController {

    @Autowired
    private GameRecordService gameRecordService;

    @RequestMapping(value = "/detail/list", method = RequestMethod.GET)
    public BaseRsp listDetail(@RequestParam(value="uid", required=false) String uid,
                    @RequestParam(value="minRewardDiamonds", required=false) String minRewardDiamonds,
                    @RequestParam(value="maxRewardDiamonds", required=false) String maxRewardDiamonds,
                    @RequestParam(value="logType", required=false, defaultValue="wish_bottle") String logType,
                    @RequestParam(value="startTime", required=false) String startTime,
                    @RequestParam(value="endTime", required=false) String endTime,
                    @RequestParam(value="orderType", required=false) String orderType,
                    @RequestParam(value="orderBy", required=false) String orderBy,
                    @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                    @RequestParam(value="limit", required=false, defaultValue="20") int limit) {

        String[] timeArray = CommonUtil.setStartTime(startTime, endTime)[0].split("-");
        String tableName = "game_record_" + timeArray[0] + "_" + timeArray[1] + "_" + timeArray[2].split(" ")[0];

        int detailCount = gameRecordService.getDetailListCount(tableName, uid, minRewardDiamonds,
                maxRewardDiamonds, logType, startTime, endTime);
        List<GameRecordDO> detailList = gameRecordService.getDetailList(tableName, uid, minRewardDiamonds,
                maxRewardDiamonds, logType, startTime, endTime, orderType, orderBy, offset, limit);
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("count", detailCount, "list", detailList));
    }

    @GetMapping("/statistic/list")
    public BaseRsp listStatistic(@RequestParam(value="uid", required=false) String uid,
                    @RequestParam(value="minGamingDiamonds", required=false) String minGamingDiamonds,
                    @RequestParam(value="maxGamingDiamonds", required=false) String maxGamingDiamonds,
                    @RequestParam(value="logType", required=false, defaultValue="wish_bottle") String logType,
                    @RequestParam(value="startTime", required=false) String startTime,
                    @RequestParam(value="endTime", required=false) String endTime,
                    @RequestParam(value="orderType", required=false) String orderType,
                    @RequestParam(value="orderBy", required=false) String orderBy,
                    @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                    @RequestParam(value="limit", required=false, defaultValue="20") int limit) {

            int statisticCount = gameRecordService.getGameStatisticListCount(logType, uid, minGamingDiamonds,
                maxGamingDiamonds, startTime, endTime);
            List<UserGamingWishBottleStatisticsDO> statisticList = gameRecordService.getGameStatisticList(logType, uid,
                minGamingDiamonds, maxGamingDiamonds, startTime, endTime, orderType, orderBy, offset, limit);
            return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("count", statisticCount, "statistic", statisticList));
    }

    @GetMapping("/daily_sum")
    public BaseRsp listDaily(@RequestParam(value="uid", required=false) String uid,
                              @RequestParam(value="minGamingDiamonds", required=false) String minGamingDiamonds,
                              @RequestParam(value="maxGamingDiamonds", required=false) String maxGamingDiamonds,
                              @RequestParam(value="gameType", required=false, defaultValue="wish_bottle") String gameType,
                              @RequestParam(value="startTime", required=false) String startTime,
                              @RequestParam(value="endTime", required=false) String endTime,
                              @RequestParam(value="orderType", required=false, defaultValue="desc") String orderType,
                              @RequestParam(value="orderBy", required=false, defaultValue="target_date") String orderBy,
                              @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                              @RequestParam(value="limit", required=false, defaultValue="20") int limit) {

        int detailCount = gameRecordService.countDaily(uid, minGamingDiamonds,
                maxGamingDiamonds, gameType, startTime, endTime);
        List<GameDailyDataDTO> detailList = gameRecordService.listDaily(uid, minGamingDiamonds,
                maxGamingDiamonds, gameType, startTime, endTime, orderType, orderBy, offset, limit);
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("count", detailCount, "list", detailList));
    }

    @RequestMapping(value = "/{gaming_type}/options/rewardItem", method = RequestMethod.GET)
    public BaseRsp listTermGameRewardItemOptions(@PathVariable(value = "gaming_type") String gamingType) {
        List<HashMap<Byte, String>> list = gameRecordService.getTermGameRewardItemOptions(gamingType);
        return new BaseRsp(ResultEnum.SUCCESS, list);
    }

    @RequestMapping(value = "/{gaming_type}/detail/list", method = RequestMethod.GET)
    public BaseRsp listTermGameDetail(@PathVariable(value = "gaming_type") String gamingType,
                                      @RequestParam(value="uid", required=false) String uid,
                                      @RequestParam(value="rewardItem", required=false) String rewardItem,
                                      @RequestParam(value="startTime", required=false) String startTime,
                                      @RequestParam(value="endTime", required=false) String endTime,
                                      @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                                      @RequestParam(value="limit", required=false, defaultValue="20") int limit) {

        String[] timeArray = CommonUtil.setStartTime(startTime, endTime);
        List<String> tableList = CommonUtil.getTableListFromDate(timeArray[0], timeArray[1], gamingType);

        int count = gameRecordService.getTermGameDetailCount(tableList, uid, rewardItem, startTime, endTime);
        List<MuchangwuyuDO> list = gameRecordService.getTermGameDetailList(tableList, uid, rewardItem, startTime, endTime, offset, limit);
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("count", count, "list", list));
    }

    @GetMapping("/{gaming_type}/statistic/list")
    public BaseRsp listTermGameStatistic(@PathVariable(value = "gaming_type") String gamingType,
                                         @RequestParam(value="uid", required=false) String uid,
                                         @RequestParam(value="startTime", required=false) String startTime,
                                         @RequestParam(value="endTime", required=false) String endTime,
                                         @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                                         @RequestParam(value="limit", required=false, defaultValue="20") int limit) {
        int count = gameRecordService.getTermGameStatisticCount(gamingType, uid, startTime, endTime);
        List<UserGamingMuchangwuyuStatisticsDO> statistic = gameRecordService.getTermGameStatisticList(gamingType, uid, startTime, endTime, offset, limit);
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("count", count, "statistic", statistic));
    }

    @GetMapping("/{gaming_type}/rewardInfo/list")
    public BaseRsp listTermGameRewardInfo(
                    @PathVariable(value = "gaming_type") String gamingType,
                    @RequestParam(value="termId", required=false) String termId,
                    @RequestParam(value="itemType", required=false) String itemType,
                    @RequestParam(value="startTime", required=false) String startTime,
                    @RequestParam(value="endTime", required=false) String endTime,
                    @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                    @RequestParam(value="limit", required=false, defaultValue="20") int limit) {
        int count = gameRecordService.getTermGameRewardInfoCount(gamingType, termId, itemType, startTime, endTime);
        List<MuchangwuyuTermDO> list = gameRecordService.getTermGameRewardInfoList(gamingType, termId,
            itemType, startTime, endTime, offset, limit);
        Map<String, Long> total = gameRecordService.getTermGameRewardInfoTotal(gamingType, termId,
            itemType, startTime, endTime);
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("count", count, "list", list, "total", total));
    }

    @GetMapping("/propExchange/list")
    public BaseRsp listMuChangWuYuExchange(@RequestParam(value="uid", required=false) String uid,
                    @RequestParam(value="originType", required=false) String originType,
                    @RequestParam(value="originId", required=false) String originId,
                    @RequestParam(value="toPropType", required=false) String toPropType,
                    @RequestParam(value="toPropId", required=false) String toPropId,
                    @RequestParam(value="startTime", required=false) String startTime,
                    @RequestParam(value="endTime", required=false) String endTime,
                    @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                    @RequestParam(value="limit", required=false, defaultValue="20") int limit) {
        int count = gameRecordService.getMuChangWuYuExchangeCount(uid, originType,
            originId, toPropType, toPropId, startTime, endTime);
        List<UserPropExchangeDO> list = gameRecordService.getMuChangWuYuExchangeList(uid, originType,
            originId, toPropType, toPropId, startTime, endTime, offset, limit);
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("count", count, "list", list));
    }

}
